package com.ttg.web.service.Impl;

import com.ttg.common.entities.BaseResult;
import com.ttg.common.enums.ErrorCodeEnums;
import com.ttg.common.enums.ResultStatusEnum;
import com.ttg.common.enums.RulesStatusEnum;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.model.dto.EditRulesStatusDto;
import com.ttg.model.dto.GetRulesListDto;
import com.ttg.model.pojo.RulesList;
import com.ttg.model.pojo.RulesType;
import com.ttg.model.vo.ResultStatusVo;
import com.ttg.model.vo.RulesListVo;
import com.ttg.web.dao.RulesListDao;
import com.ttg.web.dao.RulesTypeDao;
import com.ttg.web.dao.VipRattingConfigDao;
import com.ttg.web.service.RulesManagementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangHongYuan
 * @description
 * @date 2023/8/3 16:39
 */
@Service
public class RulesManagementServiceImpl implements RulesManagementService {
    @Autowired
    private RulesListDao rulesListDao;

    @Autowired
    private RulesTypeDao rulesTypeDao;

    @Autowired
    private VipRattingConfigDao configDao;

    @Override
    public List<RulesListVo> getRulesList(GetRulesListDto dto) {
        if (Objects.isNull(dto) || Objects.isNull(dto.getType())) {
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        List<RulesList> list = rulesListDao.allByStatusAndType(dto.getType());
        List<RulesListVo> rulesListVos = new ArrayList<>();
        list.forEach(l -> {
            RulesType byId = rulesTypeDao.getById(l.getRulesTypeId());
            RulesListVo vo = new RulesListVo();
            vo.setId(String.valueOf(l.getId()));
            vo.setName(l.getRulesName());
            vo.setTypeName(byId.getName());
            vo.setStatus(String.valueOf(l.getStatus()));
            if (Objects.nonNull(l.getUpdateTime())){
                vo.setUpdateTime(DateTimeUtil.formatDateTimeYYYYMMDD(l.getUpdateTime()));
            }


            rulesListVos.add(vo);

        });
        return rulesListVos;
    }

    @Override
    public ResultStatusVo editRulesStatus(EditRulesStatusDto dto) {

        if (Objects.isNull(dto)) {
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }

        if (StringUtils.isBlank(dto.getId())) {
            throw new BaseException(ErrorCodeEnums.TASK_RULES_ID_IS_NULL);
        }

        if (StringUtils.isBlank(dto.getStatus())) {
            throw new BaseException(ErrorCodeEnums.TASK_RULES_STATUS_IS_NULL);
        }
        RulesList rulesList = new RulesList();
        RulesList byId = rulesListDao.getById(dto.getId());
        if (Objects.isNull(byId)) {
            throw new BaseException(ErrorCodeEnums.TASK_RULES_IS_NOT_EXIST);
        }
        if (RulesStatusEnum.STOP.getValue().equals(Integer.valueOf(dto.getStatus()))) {
            rulesList.setStatus(RulesStatusEnum.STOP.getValue());
        } else if (RulesStatusEnum.OPEN.getValue().equals(Integer.valueOf(dto.getStatus()))) {
            if (configDao.countRules(byId.getId()) < 1) {
                throw new BaseException(ErrorCodeEnums.RULES_IS_NULL);
            }
            rulesList.setStatus(RulesStatusEnum.OPEN.getValue());
        } else {
            throw new BaseException(ErrorCodeEnums.TASK_RULES_STATUS_IS_ERROR);
        }
        rulesList.setUpdateTime(LocalDateTime.now());
        rulesList.setId(byId.getId());
        if (rulesListDao.updateById(rulesList)) {
            return new ResultStatusVo(ResultStatusEnum.SUCCESS.getCode());
        }

        return new ResultStatusVo(ResultStatusEnum.FAIL.getCode());
    }
}
